package com.chinaunicom.customtag;

import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;

/**
 * @author liry
 * @version 1.0
 * @date Created on 2021/11/23 20:27
 * Description: 自定义 BeanDefinition 解析器
 */

public class UserBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {

	// 获取Element 对应的类
	@Override
	protected Class<?> getBeanClass(Element element) {
		return Work.class;
	}


	@Override
	protected void doParse(Element element, BeanDefinitionBuilder builder) {
		// 解析element 元素
		String id = element.getAttribute("id");

		String name = element.getAttribute("name");

		String email = element.getAttribute("email");


		// 将解析的数据存放到 BeanDefinitionBuilder 中，等所有bean解析完成时统一注册到 BeanFactory中
		if (StringUtils.hasText(id)){
			builder.addPropertyValue("id", id);
		}

		if(StringUtils.hasText(name)){
			builder.addPropertyValue("name", name);
		}

		if (StringUtils.hasText(email)){
			builder.addPropertyValue("email", email);
		}
	}
}
